#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fun.h"

void menu2(va *a, war *b, P *c, int *n) {
    int option = 11;
    char ni[15]; // 玩家ID
    int z;
    while (1) {
        switch (option) {
            case 11:
               // system("cls");
               printf("\n\t\t--------------------------");
                printf("\n\t\t 1-玩家总排名");
                printf("\n\t\t 2-某玩家的英雄排名");
                printf("\n\t\t 3-单英雄玩家的排名");
                printf("\n\t\t 0-退出");
                printf("\n\t\t--------------------------");
                printf("\n\t\t 请选择(0-3)：");
                if(scanf("%d", &option) == 0){
        	printf("请重新输入\n");
		}
                while (getchar() != '\n');
                break;

            case 1:
                // 玩家总排名
                for (int g = 0; g < *n; g++) {
                    for (int h = g + 1; h < *n; h++) {
                        if (c[g].W * ((float)c[g].r / c[g].ca) < c[h].W * ((float)c[h].r / c[h].ca)) {
                            P temp = c[g];
                            c[g] = c[h];
                            c[h] = temp;
                        }
                    }
                }
                for (int f = 0; f < *n; f++) {
                    printf("排名 %d: %s\n", f + 1, b[f].na);
                    printf("总分: %d, 胜率: %.2f%%, 总场次: %d, 胜利场次: %d\n\n", c[f].W,(float)c[f].r / c[f].ca * 100, c[f].ca, c[f].r);
                }
                option = 11;
                break;

            case 2:
                system("cls");
                char ming[15];
                printf("输入玩家id：");
                scanf("%s", ming);
                for (int i = 0; i < *n; i++) {
                    if (strcmp(c[i].name, ming) == 0) {
                        z = i;
                        int hero_c[] = {c[z].Y1, c[z].Y2, c[z].Y3, c[z].Y4};
                        char hero_n[4][3];
						strcpy(hero_n[0], "Y1");  
						strcpy(hero_n[1], "Y2");
						strcpy(hero_n[2], "Y3");
						strcpy(hero_n[3], "Y4");
                     
                        for (int i = 0; i < 4; i++) {
                            for (int j = i + 1; j < 4; j++) {
                                if (hero_c[i] < hero_c[j]) {
                                    int temp_c = hero_c[i];
                                    hero_c[i] = hero_c[j];
                                    hero_c[j] = temp_c;

                                    char temp_n[3];
                        		strcpy(temp_n, hero_n[i]);
                        		strcpy(hero_n[i], hero_n[j]);
                        		strcpy(hero_n[j], temp_n);
                                }
                            }
                        }
                        // 输出排名
                        printf("玩家 %s 排名的英雄（得分从高到低）:\n", c[z].name);
                        for (int i = 0; i < 4; i++) {
                            printf("%s: %d\n", hero_n[i], hero_c[i]);
                        }
                    }
                }
                option = 11;
                break;

            case 3:
                system("cls");
                char mz[15];
                printf("输入英雄名称：");
                scanf("%s", mz);

                if (strcmp(mz, "Y1") == 0) {
                    for (int i = 0; i < *n; i++) {
                        for (int j = i + 1; j < *n; j++) {
                            if (c[i].Y1 < c[j].Y1) {
                                P temp = c[i];
                                c[i] = c[j];
                                c[j] = temp;
                            }
                        }
                    }
                    printf("Y1 排名的玩家（得分从高到低）:\n");
                    for (int i = 0; i < *n; i++) {
                        printf("第%d名——玩家%s得分：%d\n",i+1, c[i].name, c[i].Y1);
                    }
                } else if (strcmp(mz, "Y2") == 0) {

                    for (int i = 0; i < *n; i++) {
                        for (int j = i + 1; j < *n; j++) {
                            if (c[i].Y2 < c[j].Y2) {
                                P temp = c[i];
                                c[i] = c[j];
                                c[j] = temp;
                            }
                        }
                    }
                    printf("Y2 排名的玩家（得分从高到低）:\n");
                    for (int i = 0; i < *n; i++) {
                        printf("第%d名——玩家%s得分：%d\n",i+1, c[i].name, c[i].Y2);
                    }
                } else if (strcmp(mz, "Y3") == 0) {
                    for (int i = 0; i < *n; i++) {
                        for (int j = i + 1; j < *n; j++) {
                            if (c[i].Y3 < c[j].Y3) {
                                P temp = c[i];
                                c[i] = c[j];
                                c[j] = temp;
                            }
                        }
                    }
                    printf("Y3 排名的玩家（得分从高到低）:\n");
                    for (int i = 0; i < *n; i++) {
                        printf("第%d名——玩家%s得分：%d\n",i+1,c[i].name, c[i].Y3);
                    }
                } else if (strcmp(mz, "Y4") == 0) {
                    for (int i = 0; i < *n; i++) {
                        for (int j = i + 1; j < *n; j++) {
                            if (c[i].Y4 < c[j].Y4) {
                                P temp = c[i];
                                c[i] = c[j];
                                c[j] = temp;
                            }
                        }
                    }
                    printf("Y4 排名的玩家（得分从高到低）:\n");
                    for (int i = 0; i < *n; i++) {
                        printf("第%d名——玩家%s得分：%d\n",i+1, c[i].name, c[i].Y4);
                    }
                }
                else{
                	printf("此英雄不存在！");
				}
                option = 11;
                break;
                
            case 0:
            	printf("退出系统！");
				return;
			
			default:
				printf("无效选择，请重新选择。\n");
                break;
        }
    }
}
				
void readFile(va *a, war *b, P *c, int *n, int *zong) {
    FILE *fp = fopen("game_va.dat", "rb");
    if (fp == NULL) {
        printf("文件不存在或无法打开，将创建新文件。\n");
        *n = 0;      // 初始化玩家数量
        *zong = 0;   // 初始化总场次
        return;
    }

    // 读取玩家数量
    fread(n, sizeof(int), 1, fp);
    if (*n > CAPACITY) {
        printf("错误：玩家数量超过最大容量！\n");
        fclose(fp);
        return;
    }

    // 读取总场次
    fread(zong, sizeof(int), 1, fp);
    if (*zong > CAPACITY) {
        printf("错误：总场次超过最大容量！\n");
        fclose(fp);
        return;
    }

    fread(a, sizeof(va), *n, fp);

    fread(b, sizeof(war), *zong, fp);

    fread(c, sizeof(P), *n, fp);

    fclose(fp);
    printf("数据已从文件 game_va.dat 加载\n");
}

void writeFile(va *a, war *b, P *c, int n, int zong) {
    FILE *fp = fopen("game_va.dat", "wb");
    if (fp == NULL) {
        printf("文件无法创建或打开！\n");
        return;
    }
 
    //写玩家数量
    fwrite(&n, sizeof(int), 1, fp);

    //写入总场次
    fwrite(&zong, sizeof(int), 1, fp);

    //玩家
    fwrite(a, sizeof(va), n, fp);

    //战绩
    fwrite(b, sizeof(war), zong, fp);

    //英雄
    fwrite(c, sizeof(P), n, fp);

    fclose(fp);
    printf("数据已保存到文件 game_va.dat\n");
}